package com.xxx.yuedong.mine.service.impl;

import cn.hutool.core.map.MapUtil;
import com.xxx.yuedong.common.R;
import com.xxx.yuedong.mine.db.dao.RoleDao;
import com.xxx.yuedong.mine.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.relation.Role;
import java.util.ArrayList;
import java.util.HashMap;
@Service
@Slf4j
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    @Override
    public R list(HashMap param) {
        Integer page = MapUtil.getInt(param, "pageIndex");
        Integer length = MapUtil.getInt(param, "pageSize");
        Integer start = (page - 1) * length;
        param.put("start",start);
        param.put("length",length);
        ArrayList<HashMap> list = roleDao.list(param);
        Integer count = roleDao.listCount(param);
        return R.ok().put("list",list).put("totalCount",count);
    }

    @Override
    public R insert(HashMap param) {
        roleDao.insert(param);
        return R.ok();
    }

    @Override
    public R update(HashMap param) {
        int rows = roleDao.update(param);
        if (rows == 1){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @Override
    public R searchRoleInfo(Long id) {
        HashMap map = roleDao.searchRoleInfo(id);
        return R.ok(map);
    }

    @Override
    public R delete(Long[] ids) {
        Integer rows = roleDao.delete(ids);
        if (rows > 0){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @Override
    public R getRoleList() {
        ArrayList<HashMap> list = roleDao.getRoleList();
        return R.ok().put("options",list);
    }
}
